package it.unibz.easytravel.kml;

import it.unibz.easytravel.blogging.Placemark;

import java.util.Vector;

import com.google.android.maps.GeoPoint;

public class Parser {

	public static final String FILE_NAME = "test.kml";

	public static final String HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://earth.google.com/kml/2.2\">\n<Document>";
	public static final String FOOTER = "\n</Document></kml>";
	public static final String PLACEMARK = "Placemark";
	public static final String NAME = "name";
	public static final String DESCRITION = "description";
	public static final String STYLE_URL = "\n<styleUrl>#msn_ylw-pushpin</styleUrl>";
	public static final String POINT = "Point";
	public static final String COORDINATES = "coordinates";
	public static final String TIME_STAMP = "TimeStamp";
	public static final String WHEN = "when";
	public static final String LINE_STRING = "LineString";
	public static final String HREF = "href";
	public static final String LOOK_AT = "LookAt";
	public static final String LONGITUDE = "longitude";
	public static final String LATITUDE= "latitude";
	public static final String ALTITUDE= "altitude";
	public static final String RANGE= "range";
	public static final String TILT= "tilt";
	public static final String HEADING= "heading";
	public static final String ALTITUDE_MODE= "altitudeMode";
	public static final String VISIBILITY= "visibility";


	public static final String START_TAG = "<";
	public static final String END_TAG = "</";
	public static final String CLOSE_TAG = ">";

	public static String CreateKml(String path_name, Vector<GeoPoint> path, Vector<Placemark> placemarks){
		String result = HEADER;
		result += createName(FILE_NAME);

		int size = placemarks.size();
		for (int i = 0; i < size; i++) {
			Placemark p = placemarks.elementAt(i);
			result += createPlacemark(p.name, p.description, p.latitude, p.longitude, p.time, p.pic);
		}

		//result += "\n\n" + createPath(path_name, path);
		result += FOOTER;
		return result;
	}

	public static String CreateKml(String path_name, Vector<Placemark> placemarks){
		String result = HEADER;
		result += createName(FILE_NAME);

		int size = placemarks.size();
		for (int i = 0; i < size; i++) {
			Placemark p = placemarks.elementAt(i);
			result += createPlacemark(p.name, p.description, p.latitude, p.longitude, p.time, p.pic);
		}

		//result += "\n\n" + createPath(path_name, path);
		result += FOOTER;
		return result;
	}
	
	private static String createPlacemark(String name, String description, String latitude, String longitude, String timeStamp, String picPath){
		String result =	startTag(PLACEMARK)+
		createName(name)+
		createDescription(description) +
		createTag(VISIBILITY, "0") +
		createLookAt(longitude, latitude) +
		createPoint(longitude, latitude) + 
		createTime(timeStamp) +
		STYLE_URL;
		if(picPath!=null)
			result +=  createPicture(picPath);
		result += endTag(PLACEMARK);

		return result;
	}

	private static String createLookAt(String latitude, String longitude) {
		String result = startTag(LOOK_AT);
		result += createTag(LONGITUDE,longitude);
		result += createTag(LATITUDE,latitude);
		result += createTag(ALTITUDE,"0");
		result += createTag(RANGE,"400000");
		result += createTag(TILT,"0");
		result += createTag(HEADING,"0");
		result += createTag(ALTITUDE_MODE,"relativeToGround");

		result += endTag(LOOK_AT);
		return result;
	}

	private static String createPicture(String name) {
		return startTag(HREF) + "files/" + name + endTag(HREF);
	}

	private static String createPath(String name, Vector<GeoPoint> path){
		String result = startTag(PLACEMARK);
		result += createName(name);
		result += startTag(LINE_STRING);
		int size = path.size();
		for (int i = 0; i < size; i++) {
			result += path.elementAt(i).getLatitudeE6() + "," +
			path.elementAt(i).getLongitudeE6() + ",0 ";
		}
		result += endTag(LINE_STRING);
		result += endTag(PLACEMARK);
		return result;
	}

	private static String createName(String name) {
		return startTag(NAME) + name + endTag(NAME);
	}

	private static String createDescription(String description) {
		return startTag(DESCRITION) + description + endTag(DESCRITION);
	}

	private static String createTime(String timeStamp) {
		return startTag(TIME_STAMP) + startTag(WHEN) + 
		timeStamp + 
		endTag(WHEN) + endTag(TIME_STAMP);
	}

	private static String createPoint(String latitude, String longitude) {
		return startTag(POINT) + startTag(COORDINATES) + 
		latitude + "," + longitude + ",0" + 
		endTag(COORDINATES) + endTag(POINT);
	}

	private static String startTag(String tag){
		return "\n" + START_TAG + tag + CLOSE_TAG;
	}

	private static String endTag(String tag){
		return "\n" + END_TAG + tag + CLOSE_TAG;
	}

	private static String createTag(String tagName, String info){
		return startTag(tagName) + info + endTag(tagName);
	}
}
